// pages/main/purchaseOrder/buyfinishedgoodsList.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        userinfo: {},
        url: '',
        listData: [], // 数据列表
        totalCount: 0,
        pageSize: 10,
        pageIndex: 1,
        goodsByList: [],
        queryParams: {
            goodsno: ''
        },
        startDate: '',
        endDate: '',
        date: '',
        dateend: '',
    },
    n2: function (e) {
        var t = new Date,
            o = t.getFullYear(),
            a = t.getMonth() + 1,
            r = t.getDate();
        return "start" === e ? o -= 10 : "end" === e && (o += 10), a = a > 9 ? a : "0" + a, r = r > 9 ? r : "0" + r, "".concat(o, '-').concat(a, '-').concat(r)
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        var n = this;
        n.setData({
            startDate: this.n2("start"),
            endDate: this.n2("end"),
            date: this.n2({
                format: !0
            }),
            dateend: this.n2({
                format: !0
            }),
        });
        this.setData({
            url: getApp().globalData.apiurl,
            userinfo: getApp().globalData.userinfo
        });
        // 初始化列表
        this.getList()
        this.queryDropDown()
    },
    submitForm(e) {
        this.setData({
            totalCount: 0,
            pageSize: 10,
            pageIndex: 1,
        })
        // console.log(this.data.queryParams)
        this.getList()
    },
    // 赋值货品编号
    goodsClick(e) {
        this.data.queryParams.goodsno = e.detail.__args__[0];
    },

    // 下拉框初始数据
    queryDropDown() {
        let _that = this;
        wx.request({
            url: _that.data.url + 'goodsmaintain/goods/list',
            method: 'get',
            data: {
                type: '1'
            },
            success: res => {
                _that.setData({
                    goodsByList: res.data.rows
                })
            }
        });
    },

    // 获取采购单列表
    getList() {
        console.log("调用了查询方法")
        const pars = {
            pageNum: this.data.pageIndex,
            pageSize: this.data.pageSize,
            ...this.data.queryParams,
            timerange: {
                endPurchasetime: this.data.dateend + " 24:00:00",
                beginPurchasetime: this.data.date + " 00:00:00"
            }
        }
        wx.request({
            url: this.data.url + "buyfinishedgoods/buyfinishedgoods/list",
            method: "get",
            data: pars,
            success: res => {
                this.setData({
                    listData: res.data.rows,
                    totalCount: res.data.total / 10 + 1
                })
                console.log(res);
            },
            complete: () => {},
        });
    },
    // 修改操作
    updateBuyfinish(e) {
        console.log(e.target.dataset.e)
        wx.navigateTo({
            url: "/pages/main/purchaseOrder/buyfinishedgoodsAdd?buyId=" + e.target.dataset.e
        });
    },
    // 删除操作
    delBuyfinish(e) {
        let _that = this;
        console.log(e.target.dataset.e);
        let id = e.target.dataset.e;
        wx.showModal({
            title: '提示',
            content: '确认删除「' + e.target.dataset.name + '」采购单',
            success: function (res) {
                if (res.confirm) { //点击了确定以后
                    wx.request({
                        url: _that.data.url + 'buyfinishedgoods/buyfinishedgoods/' + id,
                        method: "DELETE",
                        success: res => {
                            if (res.data.code == 200) {
                                wx.showToast({
                                    title: '删除成功！',
                                    icon: 'success',
                                    duration: 2000
                                });
                            }
                        },
                        complete: res => {
                            _that.getList();
                            // 删除当前订单绑定的司机
                            // wx.request({
                            //     url: _that.data.url + "driverorders/driverorders/delAllByOriderId?orderid=" + id,
                            //     method: "get",
                            //     success: res => {
                            //         console.log(res)
                            //     }
                            // })
                        }
                    })
                } else {
                    console.log('用户点击取消')
                }
            }
        })
    },
    // target.dataset.buyid
    getinfo(e) {
        wx.navigateTo({
            url: "/pages/main/purchaseOrder/buyfinishedgoodsInfo?buyId=" + e.currentTarget.dataset.buyid
        });
    },
    // 开始时间
    bindDateChange(e) {
        console.log("开始时间")
        if (this.handleTime(true, e.detail.value)) return this.setData({
            date: e.detail.value,
        })
        wx.showToast({
            title: "开始时间不得大于结束时间！",
            icon: "none",
            duration: 2000
        });
    },
    // 结束时间
    bindDateChangeend(e) {
        if (this.handleTime(false, e.detail.value)) return this.setData({
            dateend: e.detail.value,
        })
        wx.showToast({
            title: "结束时间不得小于开始时间！",
            icon: "none",
            duration: 2000
        });
    },
    // 时间判断
    handleTime(state, time) {
        let _that = this;
        let t = [];
        let t2 = [];
        let enums = ''; // 结束时间
        let snum = ''; // 开始时间
        if (state) {
            t2 = time.split("-"); // 当前选中的开始时间
            t = _that.data.dateend.split("-");
        } else {
            t = time.split("-");
            t2 = _that.data.date.split("-");
        }
        t.forEach((item, index) => {
            enums += item;
            snum += t2[index];
        })
        if (parseInt(enums) == parseInt(snum)) return true;
        if (parseInt(enums) > parseInt(snum)) return true;
        return false;
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        this.getList();
    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

        this.getList();

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {
        this.setData({
            pageIndex: this.data.pageIndex + 1,
            pageSize: this.data.pageSize,
        })
        if (this.data.totalCount > this.data.pageIndex) {


            const pars = {
                pageNum: this.data.pageIndex,
                pageSize: this.data.pageSize,
                ...this.data.queryParams,
                timerange: {
                    endPurchasetime: this.data.dateend + " 24:00:00",
                    beginPurchasetime: this.data.date + " 00:00:00"
                }
            }

            console.log('分页', this.data.totalCount, "---", this.data.pageIndex);

            wx.request({
                url: this.data.url + "buyfinishedgoods/buyfinishedgoods/list",
                method: "get",
                data: pars,
                success: res => {

                    this.setData({
                        // ...this.data.listData.push(res.data.rows),
                        listData: this.data.listData.concat(res.data.rows),
                        totalCount: res.data.total / 10 + 1
                    })

                },
                complete: () => {
                    console.log(this.data.listData)
                },
            });
        }
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})